{include file="public/header"}
<div class="layui-fluid">
    <div class="layui-card">
<!--        <div class="layui-form layui-card-header layuiadmin-card-header-auto">-->
<!--            <div class="layui-form-item">-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">用户ID</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="id" placeholder="请输入用户ID" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">手机号码</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="phone" placeholder="请输入手机号码" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">-->
<!--                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>-->
<!--                    </button>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->

        <div class="layui-card-body">
<!--            <div style="padding-bottom: 10px;">-->
<!--                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>-->
<!--            </div>-->

            <div id="dept_tree"></div>
<!--            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>-->
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" data-type="edit"><i
                        class="layui-icon layui-icon-edit"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="recharge" data-type="recharge"><i
                        class="layui-icon layui-icon-edit"></i>充值</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" data-type="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="__ADMIN__/layuiadmin/layui/layui.js"></script>
<script>


    layui.use(['table', 'tree'], function () {
        var $ = layui.$
            , tree = layui.tree
            , form = layui.form
            , table = layui.table;

        //tree组件初始化
        tree.render({
            elem:'#dept_tree', //绑定元素
            data: getData(), //数据源
            id:'treeId', //树形数据的唯一标识符，用于复选框的识别
            showCheckbox: false, //是否显示复选框
            onlyIconControl: true, //是否仅通过图标进行控制（可选）
            spread: true,
            click: function(obj){
                // console.log(obj.data); //得到当前点击的节点数据
                // console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                // console.log(obj.elem); //得到当前节点元素
                // console.log(obj.data.children); //当前节点下是否有子节点
                console.log(obj.data.id);
                var i = layer.open({
                    type: 2
                    , title: '团队'
                    , content: "{:url($module . '/team2')}?user_id=" + obj.data.id
                    , maxmin: true,
                    spread: true,
                });
                layer.full(i);
            },
            templet: function(d){
                return '<div class="layui-tree-node">'+
                    '<div class="layui-tree-node-content">'+
                    '<div class="layui-tree-node-title">'+ d.title +'</div>'+
                    '<div class="layui-tree-node-extra">'+ d.title +'</div>'+
                    '</div>'+
                    '</div>';
            }
        });

        setTimeout(function(){
            // 找到所有需要默认展开的节点
            var $spreadNodes = $('#dept_tree').find('.layui-tree-main li[data-spread="true"]');

            // 触发这些节点的点击事件，从而展开它们
            $spreadNodes.click();
        }, 500); // 延迟 500 毫秒，这个值可能需要根据实际情况进行调整

        function getData() {
            var data = [];
            $.ajax({
                url: "{:url($module . '/getTree')}?user_id={$user_id}",//"getTree", //后台数据请求地址
                type: "post",
                async: false,
                success: function(result){
                    data = result;
                }
            });
            return data;
        };

        table.render({
            elem: '#LAY-user-manage'
            , url: "{:url($module . '/team2', ['user_id'=>$user_id])}"
            , method: 'Post'
            , limit: '{$pageSize}'
            , cols: [[
                {field: 'id', title: 'ID', sort: true, totalRowText: '合计'}
                // , {field: 'name', title: '名称', sort: true}
                , {field: 'pid', title: '上级ID', sort: true}
                , {field: 'phone', title: '手机号码'}
                , {field: 'spending', title: '个人业绩', totalRow: true}
                , {field: 'performance', title: '总业绩', totalRow: true}
                , {field: 'month_performance', title: '当月总业绩', totalRow: true}
                , {field: 'last_month_performance', title: '上个月总业绩', totalRow: true}

                // , {field: 'goumai_money', title: '云尚积分'}
                // , {field: 'cover_format', title: '封面', sort: true}
                // , {field: 'cover_format', title: '封面', align: 'center', templet: function (d) {
                //         return '<img src="' + d.cover_format + '" />';
                //     }
                // }
                // , {field: 'annex_zip', title: '合集', sort: true}
                // , {field: 'password', title: '解压密码', sort: true}
                // , {
                //     field: 'rec', title: '是否推荐', align: 'center', templet: function (d) {
                //         if (d.rec == 1) {
                //             return '<div><input type="checkbox" value="' + d._id.$oid + '" checked name="rec" lay-skin="switch" lay-text="推荐|否" lay-filter="rec"></div>';
                //         } else {
                //             return '<div><input type="checkbox" value="' + d._id.$oid + '" name="rec" lay-skin="switch" lay-text="推荐|否" lay-filter="rec"></div>'
                //         }
                //     }
                // }
                // , {fixed: 'right', title: '操作', toolbar: '#table-useradmin-webuser', width: 300}
            ]]
            , page: true
            , totalRow: true
        });

        //推荐开启与否
        form.on('switch(rec)', function (data) {
            console.log(data.value);
            $.ajax({
                url: "{:url($module . '/rec')}",
                data: {id: data.value, status: data.elem.checked},
                type: "Post",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    if (data.status) {
                        layer.msg(data.msg);
                        table.reload('LAY-user-manage'); //数据刷新
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (data) {
                    $.messager.alert('错误', data.msg);
                }
            });
        });

        //监听行工具事件
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            // console.log(obj)
            if (obj.event === 'del') {
                console.log(obj);
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: "{php echo $this->createWebUrl('del');}",
                        data: {
                            id: obj.data.id,
                            table: '{$module}'
                        },
                        type: "Post",
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            if (data.status) {
                                layer.msg(data.msg);
                                // location.reload(); //删除成功后再刷新
                                layer.close(index); //关闭弹层
                                table.reload('LAY-user-manage'); //数据刷新
                            } else {
                                layer.msg(data.msg);
                            }

                            // location.reload(); //删除成功后再刷新
                            // layer.close(index); //关闭弹层
                        },
                        error: function (data) {
                            $.messager.alert('错误', data.msg);
                        }
                    });
                    // obj.del();
                    // layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var i = layer.open({
                    type: 2
                    , title: '更新'
                    , content: "{:url($module . '/edit')}&id=" + data._id.$oid
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        console.log(2);

                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {

                            var field = data.field; //获取提交的字段
                            console.log(field);
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: "{:url($module . '/edit')}",
                                data: field,
                                type: "Post",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data);
                                    if (data.status) {
                                        layer.msg(data.msg);
                                        // location.reload(); //删除成功后再刷新
                                        layer.close(index); //关闭弹层
                                        table.reload('LAY-user-manage'); //数据刷新
                                    } else {
                                        layer.msg(data.msg);
                                    }

                                    // location.reload(); //删除成功后再刷新
                                    // layer.close(index); //关闭弹层
                                },
                                error: function (data) {
                                    $.messager.alert('错误', data.msg);
                                }
                            });
                            // table.reload('LAY-user-manage'); //数据刷新

                        });

                        submit.trigger('click');
                    }
                });
                layer.full(i);

                // layer.prompt({
                //     formType: 2
                //     ,value: data.email
                // }, function(value, index){
                //     obj.update({
                //         email: value
                //     });
                //     layer.close(index);
                // });
            } else if (obj.event === 'recharge') {
                var i = layer.open({
                    type: 2
                    , title: '更新'
                    , content: "{:url($module . '/recharge')}&id=" + data.id
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        console.log(2);

                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {

                            var field = data.field; //获取提交的字段
                            console.log(field);
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: "{:url($module . '/recharge')}",
                                data: field,
                                type: "Post",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data);
                                    if (data.status) {
                                        layer.msg(data.msg);
                                        // location.reload(); //删除成功后再刷新
                                        layer.close(index); //关闭弹层
                                        table.reload('LAY-user-manage'); //数据刷新
                                    } else {
                                        layer.msg(data.msg);
                                    }

                                    // location.reload(); //删除成功后再刷新
                                    // layer.close(index); //关闭弹层
                                },
                                error: function (data) {
                                    $.messager.alert('错误', data.msg);
                                }
                            });
                            // table.reload('LAY-user-manage'); //数据刷新

                        });

                        submit.trigger('click');
                    }
                });
                layer.full(i);
            }
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-user-manage');
                        layer.msg('已删除');
                    });
                });
            }
            , add: function () {
                var i = layer.open({
                    type: 2
                    , title: '添加'
                    , content: "{:url($module . '/add')}"
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        console.log(2);

                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {

                            var field = data.field; //获取提交的字段
                            console.log(field);
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: "{:url($module . '/add')}",
                                data: field,
                                type: "Post",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data);
                                    if (data.status) {
                                        layer.msg(data.msg);
                                        // location.reload(); //删除成功后再刷新
                                        layer.close(index); //关闭弹层
                                        table.reload('LAY-user-manage'); //数据刷新
                                    } else {
                                        layer.msg(data.msg);
                                    }

                                    // location.reload(); //删除成功后再刷新
                                    // layer.close(index); //关闭弹层
                                },
                                error: function (data) {
                                    $.messager.alert('错误', data.msg);
                                }
                            });
                            // table.reload('LAY-user-manage'); //数据刷新

                        });

                        submit.trigger('click');
                    }
                });
                layer.full(i);
            }
        };

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-user-manage', {
                where: field
            });
        });

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
{include file="public/footer"}
